package com.jxd.hbase.utils;


import com.jxd.entity.MesContent;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class QurryTableUntil {
  public List<MesContent> qurryTable(List<String> rowkeys, String tableName, Connection connection){
      List<MesContent> qurryTableList = new ArrayList<>();

      System.out.println(connection);
      try {
          List<Get> getList = new ArrayList();
          Table table = connection.getTable(TableName.valueOf(tableName));// 获取表
          for (String rowkey : rowkeys) {
              //把rowkey加到get里，再把get装到list中
              Get get = new Get(Bytes.toBytes(rowkey));
              get.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("code"));
              getList.add(get);
          }
          Result[] results = table.get(getList);
          int i =0;
          //重点在这，直接查getList<Get>
          for (Result result : results) {
              //对返回的结果集进行操作
              MesContent mesContent = new MesContent();
              for (Cell kv : result.rawCells()) {
                  String value = Bytes.toString(CellUtil.cloneValue(kv)).replace("'", "");
                 mesContent.setCode(value);
              }
              int end = rowkeys.get(i).indexOf("-")+1;
              String mesSignid = rowkeys.get(i).substring(0, end);
              mesContent.setMesSignId(mesSignid);
              qurryTableList.add(mesContent);
              i++;
          }
      } catch (IOException e) {
          e.printStackTrace();
          System.out.println("hbase发生错误,错误信息为" + e);
      }

      return  qurryTableList;
  }

  public List<String> qurryBwContentVal(List<String> rowkeys, String tableName, Connection connection){
          List<String> bwContentVals = new ArrayList<>();
      try {
          List<Get> getList = new ArrayList();
          Table table = connection.getTable(TableName.valueOf(tableName));// 获取表
          for (String rowkey : rowkeys) {
              //把rowkey加到get里，再把get装到list中
              Get get = new Get(Bytes.toBytes(rowkey));
              get.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("code"));
              getList.add(get);
          }
//          System.out.println("getlist=="+getList.size());
          Result[] results = table.get(getList);
          //重点在这，直接查getList<Get>

          for (Result result : results) {
              //对返回的结果集进行操作
              String value="";
              for (Cell kv : result.rawCells()) {
                   value = Bytes.toString(CellUtil.cloneValue(kv)).replace("'", "");
              }
              bwContentVals.add(value);

          }
      } catch (IOException e) {
          e.printStackTrace();
          System.out.println("hbase发生错误,错误信息为" + e);
      }
//      System.out.println("bwContentVals.size()======"+bwContentVals.size());
          return  bwContentVals;
  }
    public List<MesContent> qurryTimeAndVal(List<MesContent> rowkeys, String tableName, Connection connection){
        List<MesContent> bwContentVals = new ArrayList<>();
        try {
            List<Get> getList = new ArrayList();
            Table table = connection.getTable(TableName.valueOf(tableName));// 获取表
            for (MesContent mesContent : rowkeys) {
                //把rowkey加到get里，再把get装到list中
                Get get = new Get(Bytes.toBytes(mesContent.getRowkey()));
                get.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("code"));
                getList.add(get);
            }
            int i = 0;
            Result[] results = table.get(getList);
            //重点在这，直接查getList<Get>
            for (Result result : results) {
                //对返回的结果集进行操作
                MesContent mesContent = new MesContent();
                mesContent.setMesDate(rowkeys.get(i).getMesDate());
                mesContent.setMesTime(rowkeys.get(i).getMesTime());
                mesContent.setMesSignId(rowkeys.get(i).getMesSignId());
                mesContent.setSendDevice(rowkeys.get(i).getSendDevice());
                for (Cell kv : result.rawCells()) {
                    String value = Bytes.toString(CellUtil.cloneValue(kv)).replace("'", "");
                   mesContent.setCode(value);
                }
                bwContentVals.add(mesContent);
                i++;
            }
            System.out.println("i====="+i);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("hbase发生错误,错误信息为" + e);
        }

        return  bwContentVals;
    }
}
